define(["jquery", "easy-admin", "treetable", "iconPickerFa", "autocomplete"], function ($, ea) {

    var table = layui.table,
        treetable = layui.treetable,
        iconPickerFa = layui.iconPickerFa,
        colorpicker = layui.colorpicker,
        laydate = layui.laydate,
        autocomplete = layui.autocomplete;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'system.ces/index'+parameter,
        add_url: 'system.ces/add'+parameter,
        delete_url: 'system.ces/delete',
        edit_url: 'system.ces/edit',
        modify_url: 'system.ces/modify',
        export_url: 'system.ces/export',
    };
    var Controller = {
        index: function () {

            ea.table.render({
                init: init,
                limits:[15,20,30,40,50,60,70,80,90,100,500,1000,5000,10000],
                
                toolbar:['refresh',[
                                        {
                        text: '测试用',
                        url: 'system.ces/updates',
                        method: 'open',
                        auth: 'updates',
                        class: 'layui-btn layui-btn-success layui-btn-sm',
                        icon: 'fa fa-hourglass',
                        extend: 'data-table="' + init.table_render_id + '"',
                    },

                ],'add',
                ],
                cols: [[
                    {type: "checkbox"},
                                        {field: 'id', width: 80, title: 'ID',search:false,totalRowText: '合计'},
                    {field: 'name', title: '名称'},
                    {field: 'my_color', title: '颜色',search:false},
                    {field: 'kaiguan', title: '开关',  search: false, tips: '开启|关闭', templet: ea.table.switch},
                    {field: 'shijian', title: '时间',search:false},
                    {field: 'my_lselect', title: 'ajax下拉框'},
                    {field: 'my_rodie', title: '联动单选'},
                    {field: 'my_chkebog', title: '联动多选框'},
                    {field: 'lianselect', title: '联动下拉组',search:false},
                    {field: 'create_time', title: '创建时间',search:'range'},

                    {
                        width: 200,
                        title: '操作',
                        templet:"#right_button",
                    }
                ]],
            });

            // renderTable();

            $('body').on('click', '[data-treetable-refresh]', function () {
                renderTable();
            });

            $('body').on('click', '[data-treetable-delete]', function () {
                var tableId = $(this).attr('data-treetable-delete'),
                    url = $(this).attr('data-url');
                tableId = tableId || init.table_render_id;
                url = url != undefined ? ea.url(url) : window.location.href;
                var checkStatus = table.checkStatus(tableId),
                    data = checkStatus.data;
                if (data.length <= 0) {
                    ea.msg.error('请勾选需要删除的数据');
                    return false;
                }
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                });
                ea.msg.confirm('确定删除？', function () {
                    ea.request.post({
                        url: url,
                        data: {
                            id: ids
                        },
                    }, function (res) {
                        ea.msg.success(res.msg, function () {
                            renderTable();
                        });
                    });
                });
                return false;
            });

            ea.table.listenSwitch({filter: 'status', url: init.modify_url});

            ea.table.listenEdit(init, 'currentTable', init.table_render_id, true);

            ea.listen();
        },
        add: function () {
        var time = 0;
                    $(".aselect").bind('input propertychange',function (data){
                                            var value = $(this).val();
                                            var url = data.target.dataset.url;
                                            var show_id = data.target.dataset.contentid;
                                            var name = data.target.dataset.name
                                            var table = data.target.dataset.table
                                            var relationship = data.target.dataset.relationship
                                            // console.log(data)
                                            // console.log(show_id)
                                            // console.log(name)
                                            // console.log(table)
                                            // console.log(relationship)
                                            // console.log(url)
                                            // return
                                            time = new Date().getTime();
                                            setTimeout(function (){
                                                var newtime = new Date().getTime();
                                                if(newtime>=time+499){
                                                    $.post(url,{table:table,value:value,name:name,relationship_primary_key:relationship},function (res){
                                                        // console.log(res);
                                                        var select_show = $("#"+show_id);
                                                        select_show.empty();
                                                        if(res.data.length>0){
                                                            var str = '<dd class="xuanzes" data-value="" class="">请选择</dd>';
                                                            $.each(res.data,function (i,v){
                                                                str +='<dd class="xuanzes" data-value="' + v[relationship] + '" class="">' + v[name] + '</dd>';
                                                            })
                                                            select_show.append(str)
                                                            select_show.show();
                                                        }else{
                                                            select_show.hide();
                                                        }
                                                    })
                                                    $('body').on('click','dd',function (data){
                                                        console.log(data);
                                                        var value = data.target.dataset.value;
                                                        var text = data.target.innerText;
                                                        // $(this).parent().prev().children('input')
                                                        // console.log($(this).parent().prev().children('input'))
                                                        $.each($(this).parent().prev().children('input'),function (i,v){
                                                            if(i==1){
                                                                $(v).val(value);
                                                            }else if(i==0){
                                                                $(v).val(text);
                                                            }
                                                        })
                                                        $(this).parent().hide();
                                                    })
                                                }else{
                                                    console.log('时间没到');
                                                }
                                            },500);
                                        })
            $(".icon").each(function (i,v) {
                var ids = $(v).attr('id');
                var myiconPickerFa = iconPickerFa.render({
                    elem:"#"+ids,
                    url: PATH_CONFIG.iconLess,
                    limit: 12,
                    click: function (data) {
                        $('#'+ids).val('fa ' + data.icon);
                    },
                    success: function (d) {
                        console.log(d);
                    }
                })
            })
            $(".color").each(function (i,v) {
                var ids = $(v).attr('id');
                var coles = $('[name='+ids+']').val();
                //初始色值
               var mycolorpicker = colorpicker.render({
                    elem: '#'+ids
                    ,color: coles //设置默认色
                    ,change:function (color) {
                       $('[name='+ids+']').val(color);
                   }
                });
            })
            $(".datetime").each(function (i,v) {
                var ids = $(v).attr('id');
                laydate.render({
                    elem: '#'+ids
                    ,type: 'datetime'
                });
            })
            ea.listen(function (data) {
            console.log(data)
                return data;
            }, function (res) {
                console.log(res);
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        },
        edit: function () {
        var time = 0;
                    $(".aselect").bind('input propertychange',function (data){
                                            var value = $(this).val();
                                            var url = data.target.dataset.url;
                                            var show_id = data.target.dataset.contentid;
                                            var name = data.target.dataset.name
                                            var table = data.target.dataset.table
                                            var relationship = data.target.dataset.relationship
                                            // console.log(data)
                                            // console.log(show_id)
                                            // console.log(name)
                                            // console.log(table)
                                            // console.log(relationship)
                                            // console.log(url)
                                            // return
                                            time = new Date().getTime();
                                            setTimeout(function (){
                                                var newtime = new Date().getTime();
                                                if(newtime>=time+499){
                                                    $.post(url,{table:table,value:value,name:name,relationship_primary_key:relationship},function (res){
                                                        // console.log(res);
                                                        var select_show = $("#"+show_id);
                                                        select_show.empty();
                                                        if(res.data.length>0){
                                                            var str = '<dd class="xuanzes" data-value="" class="">请选择</dd>';
                                                            $.each(res.data,function (i,v){
                                                                str +='<dd class="xuanzes" data-value="' + v[relationship] + '" class="">' + v[name] + '</dd>';
                                                            })
                                                            select_show.append(str)
                                                            select_show.show();
                                                        }else{
                                                            select_show.hide();
                                                        }
                                                    })
                                                    $('body').on('click','dd',function (data){
                                                        console.log(data);
                                                        var value = data.target.dataset.value;
                                                        var text = data.target.innerText;
                                                        // $(this).parent().prev().children('input')
                                                        // console.log($(this).parent().prev().children('input'))
                                                        $.each($(this).parent().prev().children('input'),function (i,v){
                                                            if(i==1){
                                                                $(v).val(value);
                                                            }else if(i==0){
                                                                $(v).val(text);
                                                            }
                                                        })
                                                        $(this).parent().hide();
                                                    })
                                                }else{
                                                    console.log('时间没到');
                                                }
                                            },500);
                                        })
            $(".icon").each(function (i,v) {
                var ids = $(v).attr('id');
                var myiconPickerFa = iconPickerFa.render({
                    elem:"#"+ids,
                    url: PATH_CONFIG.iconLess,
                    limit: 12,
                    click: function (data) {
                        $('#'+ids).val('fa ' + data.icon);
                    },
                    success: function (d) {
                        console.log(d);
                    }
                })
            })
            $(".color").each(function (i,v) {
                var ids = $(v).attr('id');
                var coles = $('[name='+ids+']').val();
                //初始色值
               var mycolorpicker = colorpicker.render({
                    elem: '#'+ids
                    ,color: coles //设置默认色
                    ,change:function (color) {
                       $('[name='+ids+']').val(color);
                   }
                });
            })
            $(".datetime").each(function (i,v) {
                var ids = $(v).attr('id');
                laydate.render({
                    elem: '#'+ids
                    ,type: 'datetime'
                });
            })
            ea.listen(function (data) {
            console.log(data)
                return data;
            }, function (res) {
                console.log(res);
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        },
        details:function(){
                    var time = 0;
                            $(".aselect").bind('input propertychange',function (data){
                                                    var value = $(this).val();
                                                    var url = data.target.dataset.url;
                                                    var show_id = data.target.dataset.contentid;
                                                    var name = data.target.dataset.name
                                                    var table = data.target.dataset.table
                                                    var relationship = data.target.dataset.relationship
                                                    // console.log(data)
                                                    // console.log(show_id)
                                                    // console.log(name)
                                                    // console.log(table)
                                                    // console.log(relationship)
                                                    // console.log(url)
                                                    // return
                                                    time = new Date().getTime();
                                                    setTimeout(function (){
                                                        var newtime = new Date().getTime();
                                                        if(newtime>=time+499){
                                                            $.post(url,{table:table,value:value,name:name,relationship_primary_key:relationship},function (res){
                                                                // console.log(res);
                                                                var select_show = $("#"+show_id);
                                                                select_show.empty();
                                                                if(res.data.length>0){
                                                                    var str = '<dd class="xuanzes" data-value="" class="">请选择</dd>';
                                                                    $.each(res.data,function (i,v){
                                                                        str +='<dd class="xuanzes" data-value="' + v[relationship] + '" class="">' + v[name] + '</dd>';
                                                                    })
                                                                    select_show.append(str)
                                                                    select_show.show();
                                                                }else{
                                                                    select_show.hide();
                                                                }
                                                            })
                                                            $('body').on('click','dd',function (data){
                                                                console.log(data);
                                                                var value = data.target.dataset.value;
                                                                var text = data.target.innerText;
                                                                // $(this).parent().prev().children('input')
                                                                // console.log($(this).parent().prev().children('input'))
                                                                $.each($(this).parent().prev().children('input'),function (i,v){
                                                                    if(i==1){
                                                                        $(v).val(value);
                                                                    }else if(i==0){
                                                                        $(v).val(text);
                                                                    }
                                                                })
                                                                $(this).parent().hide();
                                                            })
                                                        }else{
                                                            console.log('时间没到');
                                                        }
                                                    },500);
                                                })
                    $(".icon").each(function (i,v) {
                        var ids = $(v).attr('id');
                        var myiconPickerFa = iconPickerFa.render({
                            elem:"#"+ids,
                            url: PATH_CONFIG.iconLess,
                            limit: 12,
                            click: function (data) {
                                $('#'+ids).val('fa ' + data.icon);
                            },
                            success: function (d) {
                                console.log(d);
                            }
                        })
                    })
                    $(".color").each(function (i,v) {
                        var ids = $(v).attr('id');
                        var coles = $('[name='+ids+']').val();
                        //初始色值
                       var mycolorpicker = colorpicker.render({
                            elem: '#'+ids
                            ,color: coles //设置默认色
                            ,change:function (color) {
                               $('[name='+ids+']').val(color);
                           }
                        });
                    })
                    $(".datetime").each(function (i,v) {
                        var ids = $(v).attr('id');
                        laydate.render({
                            elem: '#'+ids
                            ,type: 'datetime'
                        });
                    })
                    ea.listen(function (data) {
                    console.log(data)
                        return data;
                    }, function (res) {
                        console.log(res);
                        ea.msg.success(res.msg, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.$('[data-treetable-refresh]').trigger("click");
                        });
                    });
        }
    };
    return Controller;
});
